﻿package 
{
    import OpenFunctionMc.*;
    import com.assist.view.*;
    import com.assist.view.warSoul.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class OpenFunctionMc extends Sprite
    {
        private var _functionOpenPlayEnd:Function;
        private var _timer:Timer;
        private var _playNum:int = 0;
        private var _speed:Number = 1;
        private var _angle:Number = 1;
        private var _video:MovieClip;
        private var _isBlackMc:Boolean = false;
        private var _functionList:Array;
        private var _isPlay:Boolean = false;
        private var _endTargetPoint:Point;
        private var _targetData:Object;
        private var _stageWidth:int = 1250;
        private var _stageHeight:int = 650;
        private var _rect:Sprite;
        private var _black:MovieClip;

        public function OpenFunctionMc()
        {
            this._functionOpenPlayEnd = new Function();
            this._timer = new Timer(20);
            this.alpha = 0;
            this._timer.addEventListener(TimerEvent.TIMER, this.mcTime);
            return;
        }// end function

        public function set functionOpenPlayEnd(param1:Function) : void
        {
            this._functionOpenPlayEnd = param1;
            return;
        }// end function

        private function mcTime(event:TimerEvent) : void
        {
            if (this._playNum == 1)
            {
                if (this.alpha < 1)
                {
                    this.alpha = this.alpha + 0.06;
                }
                else
                {
                    this._playNum = 2;
                }
            }
            if (this._playNum == 2)
            {
                if (this._isBlackMc)
                {
                    if (this.alpha > 0)
                    {
                        this.alpha = this.alpha - 0.1;
                    }
                    else
                    {
                        this._playNum = 0;
                        this.playEnd();
                    }
                }
                else
                {
                    this.mcPlay();
                    this.isMcEnd();
                    this.isAllEnd();
                }
            }
            return;
        }// end function

        private function mcPlay() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._functionList.length;
            var _loc_2:* = _loc_1 - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this._functionList[_loc_2];
                if (_loc_3.isEnd == false)
                {
                    if (_loc_3.light)
                    {
                        this._speed = this._speed + 0.2;
                        (this._angle + 1);
                        Moving.rocketMoveForPoint(_loc_3.light, this._endTargetPoint.x, this._endTargetPoint.y, -this._speed, this._angle);
                    }
                    else if (_loc_3.mcTime <= 0)
                    {
                        if (_loc_3.mc.alpha > 0)
                        {
                            _loc_3.mc.scaleX = _loc_3.mc.scaleX + 0.1;
                            _loc_3.mc.scaleY = _loc_3.mc.scaleY + 0.1;
                            _loc_3.mc.alpha = _loc_3.mc.alpha - 0.1;
                            _loc_3.mc.x = _loc_3.mcX - (_loc_3.mc.width - _loc_3.mcW) / 2;
                            _loc_3.mc.y = _loc_3.mcY - (_loc_3.mc.height - _loc_3.mcH) / 2;
                        }
                        else
                        {
                            this.addLight(_loc_3);
                        }
                    }
                    else
                    {
                        var _loc_4:* = _loc_3;
                        var _loc_5:* = _loc_3.mcTime - 1;
                        _loc_4.mcTime = _loc_5;
                    }
                }
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

        private function addLight(param1:Object) : void
        {
            var _loc_2:* = FunctionBarEffect.light;
            var _loc_3:* = new _loc_2 as MovieClip;
            param1.light = _loc_3;
            param1.light.x = param1.mcX;
            param1.light.y = param1.mcY;
            addChild(_loc_3);
            return;
        }// end function

        private function isMcEnd() : void
        {
            var _loc_3:Object = null;
            var _loc_4:Number = NaN;
            var _loc_1:* = this._functionList.length;
            var _loc_2:* = _loc_1 - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this._functionList[_loc_2];
                if (_loc_3.light)
                {
                }
                if (_loc_3.isEnd == false)
                {
                    _loc_4 = Moving.pointDistance(_loc_3.light, this._endTargetPoint.x, this._endTargetPoint.y);
                    if (_loc_4 <= 20)
                    {
                        _loc_3.isEnd = true;
                        _loc_3.light.alpha = 0;
                    }
                }
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

        private function isAllEnd() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._functionList.length;
            var _loc_2:* = _loc_1 - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this._functionList[_loc_2];
                if (_loc_3.isEnd == false)
                {
                    return;
                }
                _loc_2 = _loc_2 - 1;
            }
            this.playVideo();
            this._timer.stop();
            return;
        }// end function

        private function playVideo() : void
        {
            this._video = FunctionBarEffect.video;
            addChild(this._video);
            this._video.x = this._endTargetPoint.x;
            this._video.y = this._endTargetPoint.y;
            this._video.addEventListener("finish", this.mcEnd);
            this._video.gotoAndPlay(2);
            return;
        }// end function

        private function mcEnd(event:Event) : void
        {
            this._isBlackMc = true;
            this._timer.start();
            return;
        }// end function

        private function playEnd() : void
        {
            this._rect.graphics.clear();
            this._timer.stop();
            this._speed = 1;
            this._angle = 1;
            this._isBlackMc = false;
            this._isPlay = false;
            this._functionOpenPlayEnd(this._targetData);
            return;
        }// end function

        public function setFunctionList(param1:Array) : void
        {
            if (this._isPlay)
            {
                return;
            }
            this._isPlay = true;
            this.alpha = 0;
            this.blackMc();
            this.drawBlack();
            this.renderActivityMc(param1);
            return;
        }// end function

        public function set setEndTarget(param1:Object) : void
        {
            this._targetData = param1;
            this._endTargetPoint = new Point(param1.mc.x - this.x + 40, param1.mc.y - this.y + 40);
            return;
        }// end function

        private function renderActivityMc(param1:Array) : void
        {
            var _loc_4:String = null;
            var _loc_5:Object = null;
            var _loc_6:MovieClip = null;
            this._functionList = [];
            var _loc_2:* = param1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = param1[_loc_3];
                _loc_5 = {};
                _loc_6 = GetActivityButton.getActivityButton(_loc_4);
                _loc_5.mcTime = 30 + 5 * _loc_3;
                _loc_5.mc = _loc_6;
                _loc_5.isEnd = false;
                _loc_5.mcW = _loc_6.width;
                _loc_5.mcH = _loc_6.height;
                addChild(_loc_6);
                this._functionList.push(_loc_5);
                _loc_3 = _loc_3 + 1;
            }
            this.sorcMc(this._functionList);
            return;
        }// end function

        private function sorcMc(param1:Array) : void
        {
            var _loc_6:Object = null;
            var _loc_7:MovieClip = null;
            var _loc_2:* = param1.length;
            var _loc_3:* = _loc_2 * 73;
            var _loc_4:* = (this._stageWidth - _loc_3) / 2;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_6 = param1[_loc_5];
                _loc_7 = _loc_6.mc;
                _loc_7.x = _loc_4 + 73 * _loc_5;
                _loc_7.y = (this._stageHeight - _loc_7.height) / 2;
                _loc_6.mcX = _loc_7.x;
                _loc_6.mcY = _loc_7.y;
                _loc_5 = _loc_5 + 1;
            }
            this._playNum = 1;
            this._timer.start();
            return;
        }// end function

        public function drawBlack() : void
        {
            var _loc_1:* = this._stageWidth;
            var _loc_2:* = this._stageHeight;
            this._rect.graphics.clear();
            this._rect.graphics.beginFill(0);
            this._rect.graphics.drawRect(0, 0, _loc_1, _loc_2);
            this._rect.graphics.endFill();
            return;
        }// end function

        private function blackMc() : void
        {
            if (this._black != null)
            {
                return;
            }
            this._black = new MovieClip();
            addChild(this._black);
            this._black.alpha = 0.7;
            this._rect = new Sprite();
            this._black.addChild(this._rect);
            return;
        }// end function

    }
}
